home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / SwingSet / DebugGraphicsPanel.java < prev    next >
Encoding:
Java Source  |  1998-12-01  |  9.9 KB  |  314 lines

  1. /*
  2.  * @(#)DebugGraphicsPanel.java    1.6 98/08/26
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. import javax.swing.*;
  16. import javax.swing.event.*;
  17. import javax.swing.text.*;
  18. import javax.accessibility.*;
  19.  
  20. import java.awt.*;
  21. import java.awt.event.*;
  22. import java.util.*;
  23.  
  24.  
  25. /**
  26.  * Debug Graphics!
  27.  *
  28.  * @version 1.6 08/26/98
  29.  * @author Jeff Dinkins
  30.  * @author Peter Korn (accessibility support)
  31.  */
  32. public class DebugGraphicsPanel extends JPanel 
  33. {
  34.     // The Frame
  35.     SwingSet swing;
  36.     JPanel components;
  37.  
  38.     JButton button = new JButton("Button");
  39.     JRadioButton radio = new JRadioButton("RadioButton");
  40.     JCheckBox check = new JCheckBox("Checkbox");
  41.     JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);
  42.     JScrollBar scrollbar = new JScrollBar(JScrollBar.HORIZONTAL, 50, 20, 0, 100);
  43.  
  44.     JSlider flashSlider = new JSlider(JSlider.HORIZONTAL, 1, 50, 10);
  45.     JCheckBox buttonCheckbox = new JCheckBox("Button");
  46.     JCheckBox radioCheckbox = new JCheckBox("RadioButton");
  47.     JCheckBox checkboxCheckbox = new JCheckBox("Checkbox");
  48.     JCheckBox sliderCheckbox = new JCheckBox("Slider");
  49.     JCheckBox scrollbarCheckbox = new JCheckBox("ScrollBar");
  50.  
  51.     DebugGraphicsListener debugGraphicsListener = new DebugGraphicsListener();
  52.     ChangeListener sliderListener;
  53.  
  54.     public DebugGraphicsPanel(SwingSet swing) {
  55.     this.swing = swing;
  56.     sliderListener = new ChangeListener() {
  57.         public void stateChanged(ChangeEvent e) {
  58.         JSlider s = (JSlider)e.getSource();
  59.         DebugGraphics.setFlashTime(s.getValue());
  60.         }
  61.     };
  62.     flashSlider.addChangeListener(sliderListener);
  63.     
  64.  
  65.     setBorder(swing.emptyBorder5);
  66.     setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
  67.  
  68.     // *************** buttons ****************
  69.     components = SwingSet.createVerticalPanel(true);
  70.     components.setBorder(swing.emptyBorder10);
  71.  
  72.         button.setEnabled(false);
  73.         radio.setEnabled(false);
  74.         radio.setSelected(true);
  75.         check.setEnabled(false);
  76.         check.setSelected(true);
  77.     
  78.     // Add buttons to buttonPanel
  79.     JPanel buttonPanel = swing.createHorizontalPanel(false);
  80.     buttonPanel.setBorder(swing.etchedBorder10);
  81.     buttonPanel.setAlignmentX(LEFT_ALIGNMENT);
  82.     buttonPanel.setAlignmentY(TOP_ALIGNMENT);
  83.  
  84.     JLabel l = (JLabel) components.add(new JLabel("Buttons"));
  85.     l.setFont(swing.boldFont);
  86.     buttonPanel.add(button);
  87.     buttonPanel.add(Box.createRigidArea(swing.hpad20));
  88.     buttonPanel.add(radio);
  89.     buttonPanel.add(Box.createRigidArea(swing.hpad20));
  90.     buttonPanel.add(check);
  91.     components.add(buttonPanel);
  92.     components.add(Box.createRigidArea(swing.vpad20));
  93.  
  94.     // *************** slider ****************
  95.  
  96.         // slider.setEnabled(false);
  97.     slider.setEnabled(false);
  98.     slider.setPaintTicks(true);
  99.     slider.setMinorTickSpacing(10);
  100.     slider.setMajorTickSpacing(40);
  101.     slider.getAccessibleContext().setAccessibleName("Sample slider");
  102.  
  103.     JPanel sliderPanel = new JPanel() {
  104.         public Dimension getMaximumSize() {
  105.         return new Dimension(super.getMaximumSize().width, 60);
  106.         }
  107.     };
  108.     sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.X_AXIS));
  109.     sliderPanel.setBorder(swing.etchedBorder10);
  110.     sliderPanel.setAlignmentX(LEFT_ALIGNMENT);
  111.     sliderPanel.setAlignmentY(TOP_ALIGNMENT);
  112.  
  113.     l = (JLabel) components.add(new JLabel("Slider"));
  114.     l.setFont(swing.boldFont);
  115.     sliderPanel.add(slider);
  116.     components.add(sliderPanel);
  117.     components.add(Box.createRigidArea(swing.vpad20));
  118.  
  119.     // *************** scrollbar ****************
  120.  
  121.         scrollbar.setEnabled(false);
  122.     scrollbar.getAccessibleContext().setAccessibleName("Sample scrollbarlider");
  123.  
  124.     JPanel scrollbarPanel = new JPanel() {
  125.         public Dimension getMaximumSize() {
  126.         return new Dimension(super.getMaximumSize().width, 60);
  127.         }
  128.     };
  129.     scrollbarPanel.setLayout(new BoxLayout(scrollbarPanel, BoxLayout.Y_AXIS));
  130.     scrollbarPanel.setBorder(swing.etchedBorder10);
  131.     scrollbarPanel.setAlignmentX(LEFT_ALIGNMENT);
  132.     scrollbarPanel.setAlignmentY(TOP_ALIGNMENT);
  133.  
  134.     JPanel box = swing.createHorizontalPanel(false);
  135.     box.add(Box.createRigidArea(swing.hpad5));
  136.     box.add(scrollbar);
  137.     box.add(Box.createRigidArea(swing.hpad5));
  138.  
  139.     l = (JLabel) components.add(new JLabel("ScrollBar"));
  140.     l.setFont(swing.boldFont);
  141.     scrollbarPanel.add(Box.createRigidArea(swing.vpad5));
  142.     scrollbarPanel.add(box);
  143.     scrollbarPanel.add(Box.createRigidArea(swing.vpad5));
  144.     components.add(scrollbarPanel);
  145.     components.add(Box.createRigidArea(swing.vpad20));
  146.  
  147.     l = (JLabel) components.add(new JLabel("Note: the above components are intentionally disabled."));
  148.     components.add(Box.createRigidArea(swing.vpad5));
  149.     l = (JLabel) components.add(new JLabel("Choose a component checkbox at right, then click on"));
  150.     l = (JLabel) components.add(new JLabel("the \"Repaint\" button to see debug graphics at work."));
  151.  
  152.  
  153.     // ***** fill out the rest of the panel
  154.     components.add(Box.createGlue());
  155.  
  156.     // *************** Create the debug graphics controls ****************
  157.     JPanel controls = new JPanel() {
  158.         public Dimension getMaximumSize() {
  159.         return new Dimension(300, super.getMaximumSize().height);
  160.         }
  161.     };
  162.     controls.setLayout(new BoxLayout(controls, BoxLayout.Y_AXIS));
  163.  
  164.     JPanel debugControls = swing.createHorizontalPanel(true);
  165.     debugControls.setAlignmentY(TOP_ALIGNMENT);
  166.     debugControls.setAlignmentX(LEFT_ALIGNMENT);
  167.  
  168.     JPanel leftColumn = swing.createVerticalPanel(false);
  169.     leftColumn.setAlignmentX(LEFT_ALIGNMENT);
  170.     leftColumn.setAlignmentY(TOP_ALIGNMENT);
  171.  
  172.     debugControls.add(leftColumn);
  173.     debugControls.add(Box.createRigidArea(swing.hpad20));
  174.     debugControls.add(Box.createRigidArea(swing.hpad20));
  175.  
  176.     controls.add(debugControls);
  177.  
  178.     // Display Options
  179.     l = new JLabel("Use Debug Graphics On:");
  180.     leftColumn.add(l);
  181.     l.setFont(swing.boldFont);
  182.  
  183.      buttonCheckbox.addItemListener(debugGraphicsListener);
  184.     buttonCheckbox.setToolTipText("Turns DebugGraphics on or off.");
  185.      leftColumn.add(buttonCheckbox);
  186.  
  187.      radioCheckbox.addItemListener(debugGraphicsListener);
  188.     radioCheckbox.setToolTipText("Turns DebugGraphics on or off.");
  189.      leftColumn.add(radioCheckbox);
  190.  
  191.      checkboxCheckbox.addItemListener(debugGraphicsListener);
  192.     checkboxCheckbox.setToolTipText("Turns DebugGraphics on or off.");
  193.      leftColumn.add(checkboxCheckbox);
  194.  
  195.      sliderCheckbox.addItemListener(debugGraphicsListener);
  196.     sliderCheckbox.setToolTipText("Turns DebugGraphics on or off.");
  197.      leftColumn.add(sliderCheckbox);
  198.  
  199.      scrollbarCheckbox.addItemListener(debugGraphicsListener);
  200.     scrollbarCheckbox.setToolTipText("Turns DebugGraphics on or off.");
  201.      leftColumn.add(scrollbarCheckbox);
  202.  
  203.     // debug flashTime
  204.     leftColumn.add(Box.createRigidArea(swing.vpad40));
  205.     l = new JLabel("Debug Flash Interval:");
  206.     l.setFont(swing.boldFont);
  207.     leftColumn.add(l);
  208.     leftColumn.add(flashSlider);
  209.     flashSlider.setMaximumSize(new Dimension(150, 60));
  210.     flashSlider.setPaintTicks(true);
  211.     flashSlider.setMinorTickSpacing(5);
  212.     flashSlider.setMajorTickSpacing(20);
  213.     flashSlider.setToolTipText("Sets the sleep time between graphics operations, from 1 to 50 milliseconds");
  214.     flashSlider.getAccessibleContext().setAccessibleName("Debug Flash Interval");
  215.  
  216.     // repaint button
  217.     leftColumn.add(Box.createRigidArea(swing.vpad40));
  218.      JButton repaintButton = new JButton("Repaint");
  219.     repaintButton.setToolTipText("Causes the selected components to be repainted using DebugGraphics.");
  220.      repaintButton.addActionListener(debugGraphicsListener);
  221.      leftColumn.add(repaintButton);
  222.      leftColumn.add(Box.createGlue());
  223.  
  224.     add(components);
  225.     add(Box.createRigidArea(swing.hpad10));
  226.      add(controls);
  227.     }
  228.  
  229.     public void resetAll() {
  230.     scrollbarCheckbox.setSelected(false);
  231.     buttonCheckbox.setSelected(false);
  232.     radioCheckbox.setSelected(false);
  233.     sliderCheckbox.setSelected(false);
  234.     checkboxCheckbox.setSelected(false);
  235.     }
  236.  
  237.     
  238.     class DebugGraphicsListener implements ItemListener, ActionListener {
  239.         boolean repaintButton = false;
  240.         boolean repaintRadio = false;
  241.         boolean repaintCheck = false;
  242.         boolean repaintSlider = false;
  243.         boolean repaintScrollBar = false;
  244.  
  245.         public void actionPerformed(ActionEvent e) {
  246.         if(e.getSource() instanceof JButton) {
  247.             if(repaintButton) {
  248.             button.repaint();
  249.             }
  250.             if(repaintRadio) {
  251.             radio.repaint();
  252.             }
  253.             if(repaintCheck) {
  254.             check.repaint();
  255.             }
  256.             if(repaintSlider) {
  257.             slider.repaint();
  258.             }
  259.             if(repaintScrollBar) {
  260.             scrollbar.repaint();
  261.             }
  262.         }
  263.         }
  264.  
  265.         public void itemStateChanged(ItemEvent e) {
  266.         AbstractButton b = (AbstractButton) e.getSource();
  267.         String label = b.getText();
  268.         if(label.equals("Button")) {
  269.             if(b.isSelected()) {
  270.             button.setDebugGraphicsOptions(DebugGraphics.FLASH_OPTION);
  271.             repaintButton = true;
  272.             } else {
  273.             button.setDebugGraphicsOptions(DebugGraphics.NONE_OPTION);
  274.             repaintButton = false;
  275.             }
  276.         } else if(label.equals("RadioButton")) {
  277.             if(b.isSelected()) {
  278.             radio.setDebugGraphicsOptions(DebugGraphics.FLASH_OPTION);
  279.             repaintRadio = true;
  280.             } else {
  281.             radio.setDebugGraphicsOptions(DebugGraphics.NONE_OPTION);
  282.             repaintRadio = false;
  283.             }
  284.         } else if(label.equals("Checkbox")) {
  285.             if(b.isSelected()) {
  286.             check.setDebugGraphicsOptions(DebugGraphics.FLASH_OPTION);
  287.             repaintCheck = true;
  288.             } else {
  289.             check.setDebugGraphicsOptions(DebugGraphics.NONE_OPTION);
  290.             repaintCheck = false;
  291.             }
  292.         } else if(label.equals("Slider")) {
  293.             if(b.isSelected()) {
  294.             slider.setDebugGraphicsOptions(DebugGraphics.FLASH_OPTION);
  295.             repaintSlider = true;
  296.             } else {
  297.             slider.setDebugGraphicsOptions(DebugGraphics.NONE_OPTION);
  298.             repaintSlider = false;
  299.             }
  300.         } else if(label.equals("ScrollBar")) {
  301.             if(b.isSelected()) {
  302.             scrollbar.setDebugGraphicsOptions(DebugGraphics.FLASH_OPTION);
  303.             repaintScrollBar = true;
  304.             } else {
  305.             scrollbar.setDebugGraphicsOptions(DebugGraphics.NONE_OPTION);
  306.             repaintScrollBar = false;
  307.             }
  308.         } else {
  309.         }
  310.     }
  311.     }
  312.  
  313. }
  314.